import { _decorator, Component, Node, Vec3, Prefab, ParticleSystemComponent, Material, ModelComponent, CCBoolean, Quat, BoxColliderComponent, ITriggerEvent, CCFloat, RigidBody } from "cc";
import { poolManager } from "../../../../Framework/Scripts/Managers/poolManager";
import { ResMgr } from "../../../../Framework/Scripts/Managers/ResMgr";
import { SoundMgr } from "../../../../Framework/Scripts/Managers/SoundMgr";
import { Vector3 } from "../../../../Framework/Scripts/Utils/Vector3";
import { OBJ_TYPE, PHY_GROUP } from "../../constant";
import { enemyBase } from "../enemyBase";
import { enemySkillBase } from "../enemySkill/enemySkillBase";
import { playerBase } from "../playerBase";

//import { PoolManager } from "../framework/poolManager";
//import { Fighter } from "./fighter";
const { ccclass, property } = _decorator;
//掷矛兵
@ccclass("Missile")
export class Missile extends enemySkillBase {
    @property(Prefab)
    public pfTrail: Prefab = null!; //拖尾



    @property(CCBoolean)
    public isAutoRotate: boolean = false;//武器是否朝向下一个目标位置


    public isHand:boolean=false//是手里的武器 不销毁


    private parentTS: enemyBase = null
    private totalTime: number = -1;//飞行时间
    private currentTime: number = 0;//当前飞行时间
    private maxHeight: number = 0;//表示此次飞行最大高度
    private posOffset: Vec3 = new Vec3();//起点和终点的向量差
    private posStart: Vec3 = new Vec3();//起点
    private endCb: Function = null!;//到达目标位置后的回调函数
    private ndTrail: Node = null!;//拖尾节点
    private rotateCoolTime: number = 0;//旋转角度的冷却时间
    private _targetPos: Vec3 = new Vec3();//当前目标位置
    private _nextTargetPos: Vec3 = new Vec3();//下次目标位置

    private _colliderCom: BoxColliderComponent = null!;//
    private dstRot: Quat = null;
    private now: Quat = new Quat();

    private targetNode: Node = null

    public howHit: Node = null

    onLoad() {

        this._colliderCom = this.node.getComponent(BoxColliderComponent) as BoxColliderComponent;

    }
    onEnable() {
        this._colliderCom.on('onTriggerEnter', this._onTriggerEnterCb, this);
    }

    onDisable() {
        this._colliderCom.off('onTriggerEnter', this._onTriggerEnterCb, this);
    }
    start() {
    }


    private _onTriggerEnterCb(event: ITriggerEvent) {




        let other = event.otherCollider.node;
        this.hit(other)
















    }
    /**
     * 重置武器数据
     */
    public reset() {
        this.endCb = null!;
        this.totalTime = 0;
        this.currentTime = 0;
        this.posStart = new Vec3();
        this.posOffset = new Vec3();
        this.maxHeight = 0;
        this.node.setScale(1, 1, 1);

        if (this.ndTrail) {
            poolManager.instance.putNode(this.ndTrail);
            this.ndTrail = null;
        }
    }

    /**
     * 向敌人投掷武器
     * @param posEnd 目标敌人位置
     * @param speed 投掷速度
     * @param attackTarget 攻击的敌人(的脚本)
     * @param endCb 
     */
    public throwMissile(posEnd: Vec3, speed: number, endCb: Function, howHit: Node, ts: enemyBase, targetNode) {
   

        this.howHit = null;
        this.howHit = howHit
        this.parentTS = ts
        this.targetNode = targetNode


        this.rotateCoolTime = 0;

        //计算出飞行路径
        const posS: Vec3 = this.node.worldPosition


        this.posStart = new Vec3(posS.x, posEnd.y, posS.z);
        this.posOffset = Vector3.sub(posEnd, this.node.worldPosition)
        this.totalTime = this.posOffset.length() / speed;
        if (this.totalTime < 0.5) {
            this.totalTime = 0.5
        }
        this.currentTime = 0;
        this.maxHeight = this.totalTime * 5;
        this.endCb = endCb;

    }

    /**
     * 飞行结束时触发
     */
    private _onFlyOver() {

        this.endCb && this.endCb();
        this.endCb = null!;

        if (this.ndTrail) {
            poolManager.instance.putNode(this.ndTrail);
            this.ndTrail = null!;
        }

        poolManager.instance.putNode(this.node);
    }



    update(dt: number) {
        if(this.isHand){
            return
        }
        // Your update function goes here.
        if (this.totalTime <= 0) {
            this._onFlyOver();
            return;
        }
        if (this.parentTS.isDie) {
            this._onFlyOver();
            return;
        }
        if (this.targetNode.getComponent(enemyBase) && this.targetNode.getComponent(enemyBase).isDie) {
            this._onFlyOver();
            return;

        }

        if (this.targetNode.getComponent(playerBase) && this.targetNode.getComponent(playerBase).isDie) {
            this._onFlyOver();
            return;
        }


        if (this.currentTime < this.totalTime) {
            this.currentTime += dt;

            if (this.currentTime > this.totalTime) {
                this.currentTime = this.totalTime;
                this._onFlyOver();
                return
            }

            //百分比
            let percent = Number((this.currentTime / this.totalTime).toFixed(2));

            //目标高度
            let height = this.maxHeight * Math.cos(percent * Math.PI - Math.PI / 2);

            //设置目标位置
            this._targetPos = new Vec3(this.posStart.x + this.posOffset.x * percent, this.posStart.y + height, this.posStart.z + this.posOffset.z * percent);

            //设置武器位置
            this.node.setWorldPosition(this._targetPos);

            if (this.isAutoRotate) {
                this.rotateCoolTime -= dt;
                if (this.rotateCoolTime < 0) {
                    this.rotateCoolTime = 0.1;

                    percent = Number(((this.currentTime + 0.1) / this.totalTime).toFixed(2));
                    if (percent < 1) {

                        //下一个目标位置的高度 
                        height = this.maxHeight * Math.cos(percent * Math.PI - Math.PI / 2);

                        //下一个目标位置
                        this._nextTargetPos = new Vec3(this.posStart.x + this.posOffset.x * percent, this.posStart.y + height, this.posStart.z + this.posOffset.z * percent)



                        this.node.forward = this._nextTargetPos.subtract(this._targetPos).normalize().negative();
                    }
                }
            }

            if (percent >= 1) {
                //表示达到目标
                this._onFlyOver();
            }
        }
    }
}
